package com.huanqi.api;

import android.util.Log;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ApiDouBanMessage {
    String url;
    public ApiDouBanMessage(String url){
        this.url=url;
    }
    public void message(DouBanMovieMessage douBanMovieMessage){
        MovieBean movieBean=new MovieBean();
        getMessage(url, new DouBanMessage() {
            @Override
            public void Callback(String message) {
                Log.i("请求多次: ",message);
                movieBean.setBiaoti(ApiDouBanMap.标题.getmessage(message));//标题
                movieBean.setFubiaoti(ApiDouBanMap.副标题.getmessage(message));//副标题
                movieBean.setDaoyan(ApiDouBanMap.导演.getmessage(message));//导演
                movieBean.setYouming(ApiDouBanMap.又名.getmessage(message));//又名
                movieBean.setDiqu(ApiDouBanMap.地区.getmessage(message));
                movieBean.setJuqing(ApiDouBanMap.剧情.getmessage(message));
                movieBean.setFenlei(ApiDouBanMap.分类.getmessage(message));
                movieBean.setNiandai(ApiDouBanMap.年代.getmessage(message));
                movieBean.setYanyuan(ApiDouBanMap.演员.getmessage(message));
                movieBean.setGuojia(ApiDouBanMap.国家.getmessage(message));
                movieBean.setYuyan(ApiDouBanMap.语言.getmessage(message));
                movieBean.setShangyingriqi(ApiDouBanMap.上映日期.getmessage(message));
                movieBean.setPainchang(ApiDouBanMap.片长.getmessage(message));
                movieBean.setId(ApiDouBanMap.ID.getmessage(message));
                movieBean.setImg(ApiDouBanMap.海报.getmessage(message));
                movieBean.setPingfen(ApiDouBanMap.评分.getmessage(message));
                movieBean.setEnglishbiaoti(HypyUtil.getPinYin(ApiDouBanMap.标题.getmessage(message)));
                douBanMovieMessage.callback(movieBean);
            }
        });
    }

    public void getMessage(String url,DouBanMessage douBanMessage) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String data=response.body().string();
                douBanMessage.Callback(data);
            }
        });
    }
}
